/*
 *  ABin.cpp
 *  libavid
 *
 *  Created by Peter on 23.02.12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */

#include "ABin.h"
#include "AStream.h"
#include "AObjDoc.h"

#include "AAttrList.h"
#include "AComposition.h"
#include "ABinViewSetting.h"

A_IMPLEMENT_CLASS( 'ABIN', ABin, AObject );

ABin::ABin()
	: AObject()
	, _objects()
{
}

void ABin::readFromStream( AStream& str, const AObjDoc& objd )
{
	UInt8 tagMark, version;
	
	str.readPod( tagMark );
	str.readPod( version );
	
	TR_ASSERT( tagMark == 0x02 );
	TR_ASSERT( version == 14 );
	
	AObjDoc::ObjectRef bvstRef;
	str.readPod( bvstRef ); 
	_binViewSetting = objd.resolveRef< ABinViewSetting >( bvstRef );
	
	str.readPod( _binUidHigh );
	str.readPod( _binUidLow );
	str.readPod( _numObjects );
	
	// Read each object
	BinObject* bobj;
	for( int i = 0; i < _numObjects; ++i )
	{
		bobj = new BinObject();
		
		AObjDoc::ObjectRef ref;
		str.readPod( ref );
		
		TR_TRACE( "ABin Object #" << i << " references composition object #" << ref << ", resolving to pointer..." );
		
		// Convert that into a pointer
		bobj->_composition = objd.resolveRef< AComposition >( ref );
		
		str.readPod( bobj->_xPos );
		str.readPod( bobj->_yPos );
		str.readPod( bobj->_keyframe );
		
		bobj->_userPlaced = str.readBool();
		_objects.push_back( bobj );
	}
	
	str.readPod( _displayMask );
	str.readPod( _displayMode );
	_sifted = str.readBool();
	
	for( int i = 0; i < 6; ++i )
	{
		str.readPod( _siftSettings[ i ]._siftMethod );
		_siftSettings[ i ]._siftString = str.readString();
		_siftSettings[ i ]._siftColumn = str.readString();
	}
	
	str.readPod( _numSortColumns );
	str.readPod( _macFont );
	str.readPod( _macFontSize );
	str.readPod( _macImageScale );
	
	_homeRect.readFromStream( str );
	_backColor.readFromStream( str );
	_foreColor.readFromStream( str );
	
	str.readPod( _qlImageScale );
	
	AObjDoc::ObjectRef attrObjRef;
	str.readPod( attrObjRef );
	_binAttr = objd.resolveRef< AAttrList >( attrObjRef );
	
	_wasIconic = str.readBool();
	
	UInt8 endTagMark;
	str.readPod( endTagMark );
	TR_ASSERT( endTagMark == 3 );
}

#define TRACE_FIELD( x ) "\t" #x ": " << x << std::endl

void ABin::dump( std::ostream& str ) const
{
	str << "ABin {" << std::endl;
	str <<     TRACE_FIELD( _binViewSetting );
	str <<     TRACE_FIELD( _binUidHigh );
	str <<     TRACE_FIELD( _binUidLow );
	str <<     TRACE_FIELD( _numObjects );
	str <<     "\t_objects[ " << _objects.size() << " ] {" << std::endl;

	ObjectList::const_iterator it = _objects.begin();
	
	for( ; it != _objects.end(); ++it )
	{
		str << "\t\tObject {" << std::endl;
		
		str << "\t\t\t_composition: " << (*it)->_composition << std::endl;
		str << "\t\t\t_xPos: " << (*it)->_xPos << std::endl;
		str << "\t\t\t_yPos: " << (*it)->_yPos << std::endl;
		str << "\t\t\t_keyframe: " << (*it)->_keyframe << std::endl;
		str << "\t\t\t_userPlaced: " << (*it)->_userPlaced << std::endl;
		
		str << "\t\t}" << std::endl;
	}
	
	str <<     "\t}" << std::endl;
	str <<     TRACE_FIELD( _displayMask );
	str <<     TRACE_FIELD( _displayMode );
	str <<     TRACE_FIELD( _sifted );   
	
	for( int i = 0; i < 6; ++i ) {
		str << "\t_siftSettings #" << i << " {" << std::endl;
		str << "\t\t_siftMethod: " << _siftSettings[ i ]._siftMethod << std::endl;
		str << "\t\t_siftString: \"" << _siftSettings[ i ]._siftString << "\"" << std::endl;
		str << "\t\t_siftColumn: \"" << _siftSettings[ i ]._siftColumn << "\"" << std::endl;
		str << "\t}" << std::endl;  
	}
	
	str <<     TRACE_FIELD( _numSortColumns );
	str <<     TRACE_FIELD( _macFont );
	str <<     TRACE_FIELD( _macFontSize );
	str <<     TRACE_FIELD( _macImageScale );
	
	str <<     "\t_homeRect: ";
	_homeRect.dump( str );
	
	str <<     "\t_backColor: ";
	_backColor.dump( str );
	
	str <<     "\t_foreColor: ";
	_foreColor.dump( str );
	
	str <<     TRACE_FIELD( _qlImageScale );
	str <<     TRACE_FIELD( _binAttr );
	str <<     TRACE_FIELD( _wasIconic );
	
	str << "}" << std::endl;
}